//
//  Palette.m
//  MapEdit
//
//  Created by Steven Canfield on 15/06/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "ColorPalette.h"

@implementation ColorPalette
- (id)initWithData:(NSData *)data
{
	self = [super init];
	
	if( self ) {
		[data getBytes:_colors];
	}
	
	return self;
}

- (NSColor *)colorAtIndex:(int)index {
	if( index >= 256 || index < 0) {
		DebugLog(@"Color Index %i Out of Palette Bounds %i", index, 256 );
		return NULL;
	}
	
	u_int32_t pixelValue = _colors[ index ];
	

//	NSLog(@"%i : %i : %i : %i", ( pixelValue ) & 255, ( pixelValue >> 8) & 255, ( pixelValue >> 16) & 255, ( pixelValue >> 24 ) & 255);
	//NSSwapLittleIntToHost( pixelValue );
	#if defined(__LITTLE_ENDIAN__)
		u_int8_t red = ( pixelValue ) & 255;
		u_int8_t green = ( pixelValue >> 8 ) & 255;
		u_int8_t blue = ( pixelValue >> 16 ) & 255;
	#else
		u_int8_t red = ( pixelValue >> 24) & 255;
		u_int8_t green = ( pixelValue >> 16 ) & 255;
		u_int8_t blue = ( pixelValue >> 8 ) & 255;
	#endif
		
	return [NSColor colorWithNormalizedRed:red green:green blue:blue];	
}

- (unsigned int *)pixelAtIndex:(int)index {
	if( index >= 256 || index < 0) {
		DebugLog(@"Color Index %i Out of Palette Bounds %i", index, 256 );
	}
	
	u_int32_t pixelValue = _colors[ index ];
	
	#if defined(__LITTLE_ENDIAN__)
		u_int8_t red = ( pixelValue ) & 255;
		u_int8_t green = ( pixelValue >> 8 ) & 255;
		u_int8_t blue = ( pixelValue >> 16 ) & 255;
	#else
		u_int8_t red = ( pixelValue >> 24) & 255;
		u_int8_t green = ( pixelValue >> 16 ) & 255;
		u_int8_t blue = ( pixelValue >> 8 ) & 255;
	#endif
	
	int alpha = 255;

	unsigned int * storage = malloc( sizeof( int ) * 4 );
	storage[0] = red;
	storage[1] = green;
	storage[2] = blue;
	storage[3] = alpha;
	return storage;
}

- (void)color:(unsigned int *)storage forPixelAtIndex:(int)index {
	if( index >= 256 || index < 0) {
		DebugLog(@"Color Index %i Out of Palette Bounds %i", index, 256 );
	}
	
	u_int32_t pixelValue = _colors[ index ];
	
#if defined(__LITTLE_ENDIAN__)
	u_int8_t red = ( pixelValue ) & 255;
	u_int8_t green = ( pixelValue >> 8 ) & 255;
	u_int8_t blue = ( pixelValue >> 16 ) & 255;
#else
	u_int8_t red = ( pixelValue >> 24) & 255;
	u_int8_t green = ( pixelValue >> 16 ) & 255;
	u_int8_t blue = ( pixelValue >> 8 ) & 255;
#endif
	
	int alpha = 255;
	
	storage[0] = red;
	storage[1] = green;
	storage[2] = blue;
	storage[3] = alpha;
}


- (void)writeToFile:(NSString *)file {
	NSImage * image = [[NSImage alloc] initWithSize:NSMakeSize( 8, 32 )];
	[image lockFocus];
	NSBitmapImageRep * imageRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
	[image unlockFocus];
	
	int x, y;
	for( x = 0; x < 8; x++ ) {
		for( y = 0; y < 32; y++ ) {
			[imageRep setColor:[self colorAtIndex:(y * 8 + x)] atX:x y:y];
		}
	}
	
	NSData * dat = [imageRep representationUsingType:NSPNGFileType properties:NULL];
	[dat writeToFile:file atomically:YES];
	
}

- (int)colorsCount {
	return 256;
}

- (void)dealloc {
	free( _colors );
	[super dealloc];
}
@end
